###############################
# Figure A2 and Tables A11-A6
###############################

rm(list=ls())

# load packages 
library(foreign)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)
library("readxl")
library(cregg)
library("ggplot2")
library(gridExtra)
library(readstata13)

##############################
# Prepare data
##############################

# load data
load("replication_data.Rdata")
names(d)

#######################################
# AMCE by Bureaucrat Gender ONLY WOMEN
#######################################

df = d[d$female_resp==1,]

amces4a <- cj(df, outcome_q3 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)[c(1:4,14:17),]
amces4a$feature = c("Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption","Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption")
diff_amces4a <- cj(df, outcome_q3 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)[7:8,]
diff_amces4a$feature = c("Speed corruption", "Theft corruption")
plot4a = plot(rbind(amces4a, diff_amces4a)) + ggplot2::facet_wrap(~BY, ncol = 3L) + ggtitle("Women respondents") + theme(plot.title = element_text(hjust = 0.5)) + geom_point(size = 1.5) + scale_y_discrete(labels=c("(Speed corruption)"="SPEED CORRUPTION","(Theft corruption)"="THEFT CORRUPTION"))  

#######################################
# AMCE by Bureaucrat Gender ONLY MEN
#######################################

dm = d[d$female_resp==0,]

amces4b <- cj(dm, outcome_q3 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)[c(1:4,14:17),]
amces4b$feature = c("Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption","Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption")
diff_amces4b <- cj(dm, outcome_q3 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)[7:8,]
diff_amces4b$feature = c("Speed corruption", "Theft corruption")
plot4b = plot(rbind(amces4b, diff_amces4b)) + ggplot2::facet_wrap(~BY, ncol = 3L) + ggtitle("Men respondents") + theme(plot.title = element_text(hjust = 0.5)) + geom_point(size = 1.5) + scale_y_discrete(labels=c("(Speed corruption)"="SPEED CORRUPTION","(Theft corruption)"="THEFT CORRUPTION")) 

#####################
# Results
#####################

# Figure A2
ff = grid.arrange(plot4a,plot4b, ncol = 1, top = "")
ggsave('figureA2.pdf', ff, width = 10, height = 10)

# Tables A11-A6
cj(df, outcome_q3 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)
cj(df, outcome_q3 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)
cj(dm, outcome_q3 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)
cj(dm, outcome_q3 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)

